package com.woniuxy.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.merchant.entity.Site;
import com.woniuxy.merchant.mapper.SiteMapper;
import com.woniuxy.order.entity.Comment;
import com.woniuxy.order.entity.OrderInfo;
import com.woniuxy.order.mapper.CommentMapper;
import com.woniuxy.order.mapper.OrderInfoMapper;
import com.woniuxy.order.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.permission.entity.Permission;
import com.woniuxy.permission.mapper.PermissionMapper;
import com.woniuxy.user.entity.UserInfo;
import com.woniuxy.user.mapper.UserInfoMapper;
import org.springframework.stereotype.Service;
import zsd.waste.dto.CommentDto;
import zsd.waste.form.HuifuCommentForm;
import zsd.waste.form.UserInsertCommentForm;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-13
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private SiteMapper siteMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private UserInfoMapper userInfoMapper;


    @Override
    public void UserInsertComment(UserInsertCommentForm form, Integer userId) throws Exception {
        OrderInfo orderInfo=orderInfoMapper.selectById(form.getOrderID());
        Site site = siteMapper.selectById(orderInfo.getSiteId());
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setContent(form.getContent());
        comment.setSiteId(site.getId());
        comment.setCreatTime(LocalDateTime.now());
        comment.setOrderId(orderInfo.getOrderId());
        comment.setOrderFraction(form.getOrderFraction());
        commentMapper.insert(comment);
    }

    @Override
    public void HuifuComment(HuifuCommentForm form, Integer userId) throws Exception {
        Comment comment1 = commentMapper.selectById(form.getCommentId());
        Comment comment = new Comment();
        comment.setCommentId(comment1.getCommentId());
        comment.setDirectorId(userId);
        comment.setUserId(comment1.getUserId());
        comment.setContent(form.getContent());
        comment.setSiteId(comment1.getSiteId());
        comment.setCreatTime(LocalDateTime.now());
        comment.setOrderId(comment1.getOrderId());
        commentMapper.insert(comment);
    }

    @Override
    public List<CommentDto> userSelectComment(Integer userId) throws Exception {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        List<CommentDto> commentDtoList = new ArrayList<>();
        queryWrapper.eq("user_id", userId)
                .orderByDesc("creat_time"); // 按创建时间降序排列
        List<Comment> list = commentMapper.selectList(queryWrapper);
        for (Comment comment:list){
            UserInfo userInfo = userInfoMapper.selectById(comment.getUserId());
            Permission permission = permissionMapper.selectById(comment.getDirectorId());
            Site site = siteMapper.selectById(comment.getSiteId());
            OrderInfo orderInfo = orderInfoMapper.selectById(comment.getOrderId());
            CommentDto dto = new CommentDto();
            dto.setContent(comment.getContent());
            dto.setUserName(userInfo.getNickName());
            dto.setSiteName(site.getSiteName());
            dto.setCreatTime(comment.getCreatTime());
            dto.setDirectorName(permission.getName());
            dto.setOrderNum(orderInfo.getOrderNumber());
            commentDtoList.add(dto);
        }
        return commentDtoList;
    }

    @Override
    public List<CommentDto> SiteSelectComment(Integer userId) throws Exception {
        Permission permission = permissionMapper.selectById(userId);
        Integer siteId = permission.getSiteId();
        List<CommentDto> commentDtoList = new ArrayList<>();
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_id", siteId)
                .orderByDesc("creat_time"); // 按创建时间降序排列
        List<Comment> list = commentMapper.selectList(queryWrapper);
        for (Comment comment:list){
            UserInfo userInfo = userInfoMapper.selectById(comment.getUserId());
            Site site = siteMapper.selectById(comment.getSiteId());
            OrderInfo orderInfo = orderInfoMapper.selectById(comment.getOrderId());
            CommentDto dto = new CommentDto();
            dto.setContent(comment.getContent());
            dto.setUserName(userInfo.getNickName());
            dto.setSiteName(site.getSiteName());
            dto.setCreatTime(comment.getCreatTime());
            dto.setDirectorName(permission.getName());
            dto.setOrderNum(orderInfo.getOrderNumber());
            commentDtoList.add(dto);
        }
        return commentDtoList;
    }
}
